import { _decorator, Animation, CCInteger, Component,  EventTouch, input, Input, UITransform, Vec2, Vec3 } from 'cc';

const { ccclass, property } = _decorator;

export const BLOCK_SIZE = 40; // 添加一个放大比

@ccclass('PlayerController')
export class PlayerController extends Component {
   
    // 激活状态
    private active: boolean = false;
    // 旋转状态
    private rotation: boolean = true;
    private _curPos: Vec3 = new Vec3();
    private _targetPos: Vec3 = new Vec3();
    private mapMaxX: number = 0;
    private mapMaxY: number = 0;

    angle: number=0;
    // 移动方向
	dir : Vec2 = new Vec2(0, 0)
    // 移动速度
	@property({ type: CCInteger })
    private Speed : number = 200;
    // 旋转速度
    private rotateSpeed: number = 120;

    onLoad() {
        // 设置节点层级
        this.node.setSiblingIndex(3)
        // this.playerBody.play('runing');
        // 获取地图尺寸
        const mapSize = this.node.parent.getChildByName('MapUI').getComponent(UITransform).contentSize;
        // 地图X坐标 - 角色模型width/2(角色出屏一半)
        const playerSize = this.node.getComponent(UITransform).contentSize;
        this.mapMaxX = (mapSize.width - playerSize.width)/2;
        this.mapMaxY = (mapSize.height -playerSize.height)/2;
    }


    setInputActive(active: boolean) {
        this.active = active;
        this.rotation = active;
        if (active) {
            input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
            input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
            input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        } else {
            input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
            input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
            input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        }
    }

    onTouchStart(event: EventTouch) {
        this.active = true;
    }
	onTouchMove(event: EventTouch) {
		const delta = event.getDelta(); //获取移动的差值
        this.rotateToFacePoint(delta)
	}
    onTouchEnd(event: EventTouch) {
        this.active = false
        // this.playerBody.getComponent(Animation).stop();
    }

    /**
     * 控制任务移动
     */
    moving(dt: number){
        // 获取角色当前的位置
		const pos = this.node.position;
        this._targetPos.x =  pos.x + this.dir.x * this.Speed  * dt ;
        this._targetPos.y = pos.y + this.dir.y * this.Speed * dt ;
         // 根据地图尺寸限制玩家最大可移动范围
        const x = Math.max(-this.mapMaxX, Math.min(this.mapMaxX, this._targetPos.x))
        const y = Math.max(-this.mapMaxY, Math.min(this.mapMaxY, this._targetPos.y))
        this.node.setPosition(x, y)
	}

    /**
     * 移动方向
     */
     rotateToFacePoint(delta) {
        // 方式一
        if(delta.x > 0){
            // 右移
			this.dir.x = 1
		}else if(delta.x < -0){
             // 左移
			this.dir.x = -1
		}else if(delta.x == 0){
             // 左右停止
			this.dir.x = 0
		}
		if(delta.y > 0){
            // 上移
			this.dir.y = 1
		}else if(delta.y < -0){
            // 下移
			this.dir.y = -1
		}else if(delta.y == 0){
            // 上下停止
			this.dir.y = 0
		}
    }

  
    update (dt: number) {
        if(this.active){
            this.moving(dt)
        }
        if(this.rotation){
            // 方式一：根据角度进行旋转
            // let angle = v2(1, 0).signAngle(this.dir) * 180/Math.PI;
            // this.node.angle = angle-90;
            // 方式二：设置自转
            this.angle += this.rotateSpeed * dt; // 更新角度
            if (this.angle >= 360) { // 如果角度超过360度，重置为0
                this.angle = 0;
            }
            this.node.angle = this.angle; // 设置节点的旋转角度
        }
       
    }

    reset() {
        this.node.getPosition(this._curPos);
        this._targetPos.set(0,0,0);
    }


}

